package com.tuanzi.loan.business.repository.galaxycard.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardInfo.LOAN_GALAXY_CARD_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardPrivilege.LOAN_GALAXY_CARD_PRIVILEGE;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardPrivilegeCdkey.LOAN_GALAXY_CARD_PRIVILEGE_CDKEY;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardPrivilegeInfo.LOAN_GALAXY_CARD_PRIVILEGE_INFO;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardPrivilegeStatus.LOAN_GALAXY_CARD_PRIVILEGE_STATUS;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardType.LOAN_GALAXY_CARD_TYPE;

import java.util.List;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.galaxycard.GalaxyCardUserPrivilegeRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardUserPrivilegeSearchRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardUserPrivilegeSearchResponse;

@Repository
public class GalaxyCardUserPrivilegeRepositoryImpl extends JOOQBaseRepositoryPlus implements GalaxyCardUserPrivilegeRepository {

    @Override
    public List<String> getPrivilegeCDKeyByPrivilegeId(Long privilegeId) {
        return dslContext.select(LOAN_GALAXY_CARD_PRIVILEGE_CDKEY.ACTIVE_CODE)
                .from(LOAN_GALAXY_CARD_PRIVILEGE_CDKEY)
                .where(LOAN_GALAXY_CARD_PRIVILEGE_CDKEY.PRIVILEGE_ID.eq(privilegeId))
                .fetch(LOAN_GALAXY_CARD_PRIVILEGE_CDKEY.ACTIVE_CODE);
    }

    /**
     * GalaxyCardUserPrivilege查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    public Page<GalaxyCardUserPrivilegeSearchResponse> list(GalaxyCardUserPrivilegeSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_GALAXY_CARD_TYPE.NAME.as("CardName"));
        fields(LOAN_GALAXY_CARD_INFO.NO,
                LOAN_GALAXY_CARD_INFO.NAME.as("UserName"),
                LOAN_GALAXY_CARD_INFO.PHONE_NUMBER);
        fields(LOAN_GALAXY_CARD_PRIVILEGE.NAME);
        fields(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.SHOW);
        fields(LOAN_GALAXY_CARD_PRIVILEGE_INFO.ID,
                LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_ID,
                LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_STATUS,
                LOAN_GALAXY_CARD_PRIVILEGE_INFO.EXPRESS_ID,
                LOAN_GALAXY_CARD_PRIVILEGE_INFO.BUSINESS_ID,
                LOAN_GALAXY_CARD_PRIVILEGE_INFO.CODE.as("PrivilegeCode"));
        innerJoin(LOAN_GALAXY_CARD_INFO, LOAN_GALAXY_CARD_INFO.ID.eq(LOAN_GALAXY_CARD_PRIVILEGE_INFO.CARD_INFO_ID));
        innerJoin(LOAN_GALAXY_CARD_TYPE, LOAN_GALAXY_CARD_TYPE.ID.eq(LOAN_GALAXY_CARD_INFO.TYPE));
        innerJoin(LOAN_GALAXY_CARD_PRIVILEGE, LOAN_GALAXY_CARD_PRIVILEGE.ID.eq(LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_ID));
        innerJoin(LOAN_GALAXY_CARD_PRIVILEGE_STATUS, LOAN_GALAXY_CARD_PRIVILEGE_STATUS.PRIVILEGE_ID.eq(LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_ID)
                .and(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.STATUS.eq(LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_STATUS)));
        eq(LOAN_GALAXY_CARD_INFO.PHONE_NUMBER,searchRequest.getPhoneNumber());
        eq(LOAN_GALAXY_CARD_INFO.NO,searchRequest.getCardNo());
        eq(LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_ID, searchRequest.getPrivilege());
        eq(LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_STATUS, searchRequest.getStatus());
        return search(pageable, record -> {
            GalaxyCardUserPrivilegeSearchResponse response = new GalaxyCardUserPrivilegeSearchResponse();
            response.setId(record.get(LOAN_GALAXY_CARD_PRIVILEGE_INFO.ID));
            response.setPk(record.get(LOAN_GALAXY_CARD_PRIVILEGE_INFO.ID));
            response.setCardType(record.get("CardName",String.class));
            response.setCardNo(record.get(LOAN_GALAXY_CARD_INFO.NO));
            response.setName(record.get("UserName",String.class));
            
            String phone = record.get(LOAN_GALAXY_CARD_INFO.PHONE_NUMBER);
            response.setPhoneNumber(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            
            response.setPrivilegeId(record.get(LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_ID));
            response.setPrivilege(record.get(LOAN_GALAXY_CARD_PRIVILEGE.NAME));
            response.setStatus(record.get(LOAN_GALAXY_CARD_PRIVILEGE_INFO.PRIVILEGE_STATUS));
            response.setStatusStr(record.get(LOAN_GALAXY_CARD_PRIVILEGE_STATUS.SHOW));
            response.setCode(record.get("PrivilegeCode",String.class));
            response.setExpressId(record.get(LOAN_GALAXY_CARD_PRIVILEGE_INFO.EXPRESS_ID));
            response.setBusinessId(record.get(LOAN_GALAXY_CARD_PRIVILEGE_INFO.BUSINESS_ID));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_GALAXY_CARD_PRIVILEGE_INFO;
    }
}
